﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FbKPI.Facade;
using FbKPI.Model;

namespace FBKPI.Forms
{
    public partial class KPIForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                try
                {
                    int ID = Convert.ToInt16(Request.QueryString["ID"]);
                    txt_year.Text = DateTime.Now.ToString("yyyy");
                    if (ID != 0)
                    {
                        KPIFacade df = new KPIFacade();
                        KPI KPIs = df.GetKPIByID(ID);

                        if (KPIs != null)
                        {
                            txt_id.Text = KPIs.ID.ToString();
                            hfId.Value = KPIs.ID.ToString();
                            txt_description.Text = KPIs.Description;
                            chk_active.Checked = KPIs.isActive;
                            chk_weightenableflag.Checked = KPIs.WeightEnableFlag;
                            chk_uomenableflag.Checked = KPIs.UoMEnableFlag;
                            chk_scoredbysystem.Checked = KPIs.ScoredBySystem;
                            txt_groupkpi.Text = KPIs.GroupKPI == null ? "" : KPIs.GroupKPI.Description;
                            txt_groupkpiid.Value = KPIs.GroupKPI == null ? "" : KPIs.GroupKPI.ID.ToString();
                            txt_categorykpi.Text = KPIs.CategoryKPI == null ? "" : KPIs.CategoryKPI.Description;
                            txt_categorykpiid.Value = KPIs.CategoryKPI == null ? "" : KPIs.CategoryKPI.ID.ToString();
                            txt_typekpi.Text = KPIs.TypeKPI == null ? "" : KPIs.TypeKPI.Description;
                            txt_typekpiid.Value = KPIs.TypeKPI == null ? "" : KPIs.TypeKPI.ID.ToString();
                            txt_subtypekpi.Text = KPIs.SubTypeKPI == null ? "" : KPIs.SubTypeKPI.Description;
                            txt_subtypekpiid.Value = KPIs.SubTypeKPI == null ? "" : KPIs.SubTypeKPI.ID.ToString();
                            txt_uom.Text = KPIs.UoM == null ? "" : KPIs.UoM.Description;
                            txt_uomid.Value = KPIs.UoM == null ? "" : KPIs.UoM.ID.ToString();
                            txt_uommax.Text = KPIs.UoMDetailMax == null ? "" : KPIs.UoMDetailMax.Description;
                            txt_uommaxid.Value = KPIs.UoMDetailMax == null ? "" : KPIs.UoMDetailMax.ID.ToString();
                            txt_uommin.Text = KPIs.UoMDetailMin == null ? "" : KPIs.UoMDetailMin.Description;
                            txt_uomminid.Value = KPIs.UoMDetailMin == null ? "" : KPIs.UoMDetailMin.ID.ToString();
                            //txt_kpiparent.Text =  KPIs.KPIParent == null ? "" : KPIs.KPIParent.Description;
                            //txt_kpiparentid.Value =  KPIs.KPIParent == null ? "" : KPIs.KPIParent.ID.ToString();
                            txt_kpireplacement.Text = KPIs.KPIReplacement == null ? "" : KPIs.KPIReplacement.Description;
                            txt_kpireplacementid.Value = KPIs.KPIReplacement == null ? "" : KPIs.KPIReplacement.ID.ToString();
                            txt_year.Text = KPIs.Year.ToString();
                            txt_weight.Text = KPIs.Weight.ToString();
                            //txt_periode.Text = KPIs.Periode;
                            //txt_polarization.Text = KPIs.Polarization;
                            txt_targetdescription.Text = KPIs.TargetDescription;
                            txt_manualcalculation.Text = KPIs.ManualCalculation.ToString();
                            txt_calculationtype.Text = KPIs.KPICalculationType;
                            txt_replacementdate.Text = KPIs.ReplacementDate == null ? "" : Convert.ToDateTime(KPIs.ReplacementDate).ToShortDateString();
                            txt_replacementdescription.Text = KPIs.ReplacementDescription;
                            txt_detailinformation.Text = KPIs.KPIDetailInformation;
                            txt_strategicobjective.Text = KPIs.KPIStrategicObjective;
                            //txt_lastmodifieddate.Text = KPIs.LastModifiedDate == null ? "" : Convert.ToDateTime(KPIs.LastModifiedDate).ToShortDateString();
                            //txt_lastmodifiedby.Text = KPIs.LastModifiedBy;
                            //txt_createdate.Text = KPIs.CreateDate == null ? "" : Convert.ToDateTime(KPIs.CreateDate).ToShortDateString();
                            //txt_createby.Text = KPIs.CreateBy;
                            txtSequence.Text = KPIs.Sequence.ToString();
                            txt_superiorid.Value = KPIs.EmployeeIDVerifier.ToString();

                            Employee obj = new EmployeeFacade().GetEmployeeByID(Convert.ToInt32(txt_superiorid.Value));
                            if (obj != null)
                                txt_superior.Text = obj.FirstName + " " + obj.LastName;

                            //ddlPeriode.Items.FindByValue(KPIs.Periode.ToString()).Selected = true;
                            txt_periode.Text = KPIs.Periode.Trim();
                            ddlPolarization.Items.FindByValue(KPIs.Polarization.ToString()).Selected = true;

                            chk_lockWeight.Checked = KPIs.isLockWeight;
                            if (KPIs.isLockWeight)
                            {
                                ddlLockWeight.Enabled = false;
                            }

                            txtPrecentageMax.Text = KPIs.PercentageMax.ToString();
                            txtPrecentageMin.Text = KPIs.PercentageMin.ToString();

                            if (KPIs.SignEmployeeID != 0)
                            {
                                obj = new EmployeeFacade().GetEmployeeByID(KPIs.SignEmployeeID);
                                if (obj != null)
                                {
                                    txtEmployeeSign.Text = obj.FirstName;
                                    txtEmployeeSignID.Value = obj.ID.ToString();
                                }
                            }

                        }
                    }
                    else
                    {
                        txt_id.Text = "0";
                    }
                }
                catch
                {
                }

                //if (txt_id.Text == "0") btn_delete.Visible = false;
            }
        }
    }
}